package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.system.mapper.SysCompanyUserRoleMapper;
import com.nineclock.system.mapper.SysRoleMapper;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysCompanyUserRole;
import com.nineclock.system.pojo.SysRole;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SysCompanyUserRoleService;
import com.nineclock.system.service.SysCompanyUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysCompanyUserRoleServiceImpl extends ServiceImpl<SysCompanyUserRoleMapper, SysCompanyUserRole> implements SysCompanyUserRoleService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysCompanyUserService companyUserService;
    /**
     * 更换主管理员
     * @param map
     */
    @Override
    public void changeAdmin(Map<String, String> map) {
        if (map == null || map.size() ==0){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        String code = map.get("code");
        String id = map.get("userId");
        if (StringUtils.isBlank(code)||StringUtils.isBlank(id)){

        }
        Long userId = Long.valueOf(id);
//        SysUser sysUser = userMapper.selectById(userId);
//        String mobile = sysUser.getMobile();

        SysCompanyUser companyUser = companyUserService.getById(userId);
        String mobile = companyUser.getMobile();
        String redisCode = redisTemplate.opsForValue().get(SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX + mobile).toString();
        if (StringUtils.isBlank(redisCode)){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }
        if (!redisCode.equals(code)){
            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
        }
        Long companyId = CurrentUserHolder.get().getCompanyId();

        //查询当前企业的管理员角色id
        LambdaQueryWrapper<SysRole> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(SysRole::getCompanyId,companyId).eq(SysRole::getRoleName, NcConstant.ADMIN_ROLE_SYS);
        SysRole sysRole = roleMapper.selectOne(roleWrapper);
        Long roleId = sysRole.getId();



        //删除旧的关联关系
        LambdaQueryWrapper<SysCompanyUserRole> companyUserRoleWrapper = new LambdaQueryWrapper<>();
        companyUserRoleWrapper.eq(SysCompanyUserRole::getRoleId,roleId).eq(SysCompanyUserRole::getCompanyId,companyId);
        this.remove(companyUserRoleWrapper);
        //新的关联关系
        SysCompanyUserRole companyUserRole = new SysCompanyUserRole();
        companyUserRole.setCompanyId(companyId);
        companyUserRole.setCompanyUserId(userId);
        companyUserRole.setRoleId(roleId);
        this.save(companyUserRole);

        redisTemplate.delete(SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX + mobile);
    }
}
